class ObjId {
    constructor(obj, key, allowEmpty = false) {
        this.id = obj[key];
        this.key = key;
        this.allowEmpty = allowEmpty;
    }

    check() {
        let id = this.id;
        // 判断id格式是否正确
        if (id) {
            try {
                id = parseInt(id);
                if (isNaN(id)) {
                    return { success: false, err: `${ this.key }格式错误` };
                }
            } catch (err) {
                return { success: false, err: `${ this.key }格式错误` };
            }
        } else if (!this.allowEmpty) {
            // 查看id是否允许为空
            return { success: false, msg: `${ this.key }为空` };
        }
        return { success: true };
    }
}

module.exports = ObjId;